package com.marry.mybatis;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author ml
 * @create 2017-11-07--15:22
 */
public class MyMapperProxy implements InvocationHandler {

    private MySqlSession sqlSession;
    public MyMapperProxy(){

    }

    public MyMapperProxy(MySqlSession sqlSession){
        this.sqlSession=sqlSession;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

        String mapperClass=method.getDeclaringClass().getName();
        if(UserMapperXML.namespace.equals(mapperClass)){
            String methodName=method.getName();
            String originSql=UserMapperXML.getMethodSQL(methodName);

            String formattedSql= String.format(originSql,String.valueOf(objects[0]));

            return sqlSession.selectOne(formattedSql);
        }
        return null;
    }
}
